#include <bits/stdc++.h>
// 2025/06/09
// tag: 
// Author: Forever
using namespace std;
using int64 = int64_t;

const int N = 16001;

int n, beauty[N], f[N];
vector<int> edge[N];

void dfs(int u, int fa){
    f[u] = beauty[u];
    if (edge[u].size() == 1 && edge[u][0] == fa)
        return ;
    for (int v : edge[u]){
        if (v == fa)
            continue;
        dfs(v, u);
        f[u] += max(f[v], 0);
    } 
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> beauty[i];
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    dfs(1, 0);
    int ans = INT_MIN;
    for (int i = 1; i <= n; i++)
    {
        ans = max(ans, f[i]);
    }
    cout << ans << endl;
    return 0;
}